package com.lhl.fw.db.repository;

import com.lhl.fw.db.dsl.QueryDslContext;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.EntityPathBase;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

import javax.persistence.EntityManager;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @className: BaseJpaRepository
 * @description: 扩展自定义查询
 * @author: king
 * @date: 2020-09-03 15:25
 **/
@NoRepositoryBean
public interface BaseJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {

    /**
     * @Description: 获得实体管理器
     * @Author: king
     * @Date: 2020-06-15 18:01
     * @return: javax.persistence.EntityManager
     **/
    EntityManager getEntityManager();

    /**
     * @Description: 获得JPAQueryFactory工厂
     * @Author: king
     * @Date: 2020-06-15 18:01
     * @return: com.querydsl.jpa.impl.JPAQueryFactory
     **/
    JPAQueryFactory getJpaQueryFactory();

    /**
     * @param predicate:
     * @param pageable:
     * @param orders:
     * @Description: 返回分页数据
     * @Author: king
     * @Date: 2020-06-15 18:02
     * @return: org.springframework.data.domain.Page<T>
     **/
    Page<T> findAll(Predicate predicate, Pageable pageable, OrderSpecifier<?>... orders);

    /**
     *
     * @param pageable 分页
     * @param predicateList 查询条件
     * @param entityPathBase 查询表
     * @param orderSpecifierList 排序条件
     * @return
     */
    Page<T> queryDsl(Pageable pageable, List<Predicate> predicateList, EntityPathBase<T> entityPathBase, List<OrderSpecifier> orderSpecifierList);

    /**
     * @param predicate:      查询条件
     * @param entityPathBase: 查询表
     * @Description: 查询单个
     * @Author: king
     * @Date: 2020-07-03 15:14
     * @return: T
     **/
    T queryOneDsl(Predicate predicate, EntityPathBase<T> entityPathBase);

    /**
     * @param qdc:      queryDsl 查询对象
     * @param pageable 分页
     * @Description: 多表联合查询
     * @Author: king
     * @Date: 2020-07-03 15:16
     * @return: com.lhl.fw.rdb.dsl.PageListMapResult
     **/
    Page<Map<String, Object>> queryDslForPageListResult(QueryDslContext qdc, Pageable pageable);
}